const Base = require("./base");

cc.Class({
    extends: Base,

    unuse(){
        $T.setSprite( this.node , null );
        return this;
    },
    reuse(){
        $T.setSprite( this.node , null );
        return this;
    },
    //逐帧播放动画
    playFreameAnimate( frs , isForever ){
        frs = frs || [];
        return new Promise(( res , rej )=>{
            let animate = cc.tween(this.node);
            for(let i in frs){
                let fr = frs[i].frame,
                    size = frs[i].size,
                    scale = frs[i].scale || 1,
                    param = frs[i].param , 
                    time = $T.isType( frs[i].time , "Number") ? frs[i].time : 0 , 
                    callfn = frs[i].callfn,
                    rotation = frs[i].rotation ? frs[i].rotation : 0,
                    opacity = frs[i].opacity ? frs[i].opacity : 255;

                if($T.isType( rotation , "Array")){
                    param.rotation = rotation[1];
                }
                if($T.isType( opacity , "Array")){
                    param.opacity = opacity[1];
                }
                if(fr) animate.call(()=>{
                    $T.setSprite( this.node , fr , size );
                    this.node.rotation = $T.isType( rotation , "Array") ? rotation[0] : rotation;
                    this.node.opacity = $T.isType( opacity , "Array") ? opacity[0] : opacity;
                })
                if(scale){
                    animate.call(()=>{
                        this.node.scale = $T.isType( scale , "Array") ? scale[0] : scale;
                    })
                    param.scale = $T.isType( scale , "Array") ? scale[1] : scale;
                }

                animate.to( time , param );
                if(callfn) animate.call(()=>{
                    callfn( this.node )
                })
            }

            animate.call(()=>{
                this.node.__my_sigleAnimate = animate;
                res( this.node );
            })
            isForever && animate.repeatForever();
            animate.start();

            this._my_animate = animate;
        });
    },

    stopAnimate(){
        this._my_animate && this._my_animate.stop();
        return this;
    },

    startAnimate(){
        this._my_animate && this._my_animate.start();
        return this;
    }
});
